﻿using ISSMoviePlex.DAL.Models;
using ISSMoviePlex.DAL.Repositories;
using System.Collections.Generic;
using System.Linq;

namespace ISSMoviePlex.BLL
{
    public class MemberBLL
    {
        private readonly MemberRepository _memberRepository;
        private MemberStateBLL state;
        private Member member;

        public MemberBLL()
        {
            _memberRepository = MemberRepository.Instance;
            _memberRepository.Initialize();
        }

        public MemberBLL(int memberId) : this()
        {
            member = _memberRepository.FindById(memberId);
            switch (member.MemberState.MemberStateName)
            {
                case "Standard":
                    state = new StandardStateBLL();
                    break;
                case "Silver":
                    state = new SilverStateBLL();
                    break;
                case "Gold":
                    state = new GoldStateBLL();
                    break;
                case "Platinum":
                    state = new PlatinumStateBLL();
                    break;
                default:
                    state = new StandardStateBLL();
                    break;
            }
        }

        public List<Member> GetAllMembers()
        {
            return _memberRepository.GetAll().ToList();             
        }

        public Member GetMemberById(int memberId)
        {
            return _memberRepository.FindById(memberId);
        }

        public void AddMember(Member member)
        {
            _memberRepository.Add(member);
            _memberRepository.Save();
        }

        public void UpdateMember(Member member)
        {
            _memberRepository.Edit(member);
            _memberRepository.Save();
        }

        public void DeleteMember(Member member)
        {
            _memberRepository.Delete(member);
            _memberRepository.Save();
        }

        public double Spend(double spendAmount)
        {
            if (member != null)
            {
                double discount = member.MemberState.DiscountPercent;
                member.MoneySpent += spendAmount * (1 - discount / 100);
                state.Check(member);
                _memberRepository.Save();
                return discount;
            }
            return 0;
        }
    }
}
